<template>
  <div>
    <el-dialog
      :title="readonly ? '查看' : form.id ? '编辑' : '新增'"
      :visible.sync="show"
      :before-close="closeDialog"
      :close-on-click-modal="true"
      width="900px"
      custom-class="dialog-dliveryDetail user-dialog-css"
      center
    >
      <el-form ref="form" v-loading="loading" :model="form" label-position="left" label-width="80px">
        <el-form-item v-if="form.id" label="ID">
          <el-col :span="12">
            <el-input v-model="form.id" :disabled="true" />
          </el-col>
        </el-form-item>
        <el-form-item label="用品名" prop="name" :rules="{ required: true, message: '请输入用品名', trigger: 'blur' }">
          <el-input v-model="form.name" :disabled="readonly" />
        </el-form-item>
        <el-form-item
          label="价格"
          prop="price"
          :rules="[
            { required: true, message: '请输入价格', trigger: 'blur' },
            { pattern: /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/, message: '请输入正确的金额', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.price" :disabled="readonly" />
        </el-form-item>
        <el-form-item label="种类" prop="class" :rules="{ required: true, message: '请输入种类', trigger: 'blur' }">
          <el-input v-model="form.class" :disabled="readonly" />
        </el-form-item>
        <el-form-item
          label="总数"
          prop="sum"
          :rules="[
            { required: true, message: '请输入种类', trigger: 'blur' },
            { type: 'number', message: '种类必须为数字值' }
          ]"
        >
          <el-input v-model.number="form.sum" :disabled="readonly" />
        </el-form-item>
        <el-form-item
          label="现有库存"
          prop="rep"
          :rules="[
            { required: true, message: '请输入现有库存', trigger: 'blur' },
            { type: 'number', message: '现有库存必须为数字值' }
          ]"
        >
          <el-input v-model.number="form.rep" :disabled="readonly" />
        </el-form-item>
      </el-form>
      <span v-if="!readonly" slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { equip } from '@/api/equip'

export default {
  name: 'ListDialog',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => {}
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      loading: false
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.form = JSON.parse(JSON.stringify(this.data))
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true
          if (this.form.id) {
            await equip.update({ ...this.form, sessionId: this.$store.state.user.session_id })
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            await equip.put({ ...this.form, sessionId: this.$store.state.user.session_id })
            this.$message({
              message: '新增成功',
              type: 'success'
            })
          }

          this.loading = false
          this.afterSubmit()
        } else {
          return false
        }
      })
    },
    afterSubmit() {
      this.$emit('afterSubmit')
    },
    closeDialog() {
      this.$emit('closeDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input.is-disabled .el-input__inner {
    color: #606266;
    background-color: #fff;
}
::v-deep .el-dialog--center .el-dialog__body {
    padding: 25px 60px 25px;
}
.form-row {
    display: flex;
    justify-content: space-between;
    .el-input {
        width: 210px;
    }
    &.form-row-middle {
        .el-input {
            width: 300px;
        }
    }
}
</style>
